﻿namespace PMS.Client.FrmReport
{
    using PMS.BLL;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class FrmUserReportDesign : Form
    {
        private UserReportBLL bll = new UserReportBLL();
        private Button btnCancel;
        private Button btnSave;
        private Button btnTest;
        private CheckedListBox cbColumnsList;
        private IContainer components = null;
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
        private Label label5;
        private UserReport model = null;
        private RadioButton rbBusiness;
        private RadioButton rbOffice;
        private int reportID = 0;
        private TextBox ReportName;
        private TextBox selectSql;
        private CheckBox ShowDate;
        private CheckBox ShowOrganName;
        private CheckBox ShowTel;
        private CheckBox ShowUserName;

        public FrmUserReportDesign(int reportID = 0)
        {
            this.InitializeComponent();
            List<string> source = new List<string>();
            this.reportID = reportID;
            if (reportID > 0)
            {
                this.model = this.bll.GetModel(reportID);
                this.ReportName.Text = this.model.ReportName;
                this.ShowDate.Checked = this.model.ShowDate == 1;
                this.ShowTel.Checked = this.model.ShowUserTel == 1;
                this.ShowUserName.Checked = this.model.ShowUserName == 1;
                this.ShowOrganName.Checked = this.model.ShowOrganName == 1;
                this.selectSql.Text = this.model.SelectSql;
                this.rbOffice.Checked = this.model.IsOffice == 1;
                this.rbBusiness.Checked = this.model.IsOffice == 2;
                source = this.model.ShowColumns.Split(new char[] { ',' }).ToList<string>();
            }
            List<PropertyInfo> propList = new User().GetType().GetProperties().ToList<PropertyInfo>();
            Func<string, bool> predicate = null;
            for (int i = 0; i < propList.Count; i++)
            {
                PropertyInfo info = propList[i];
                this.cbColumnsList.Items.Add(propList[i].Name);
                if (predicate == null)
                {
                    predicate = f => f == propList[i].Name;
                }
                this.cbColumnsList.SetItemChecked(i, source.FirstOrDefault<string>(predicate) != null);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.ReportName.Text.Trim() != "")
            {
                string str = "";
                foreach (string str2 in this.cbColumnsList.CheckedItems)
                {
                    str = str + str2 + ",";
                }
                str = str.Substring(0, str.Length);
                UserReport report = new UserReport {
                    ReportName = this.ReportName.Text,
                    AddDate = DateTime.Now,
                    IsOffice = this.rbOffice.Checked ? 1 : 2,
                    SelectSql = this.selectSql.Text,
                    ShowColumns = str,
                    ShowDate = this.ShowOrganName.Checked ? 1 : 0,
                    ShowUserName = this.ShowUserName.Checked ? 1 : 0,
                    ShowOrganName = this.ShowOrganName.Checked ? 1 : 0,
                    ShowUserTel = this.ShowTel.Checked ? 1 : 0,
                    ReportID = this.reportID
                };
                this.model = report;
                if (this.reportID > 0)
                {
                    if (this.bll.Update(this.model))
                    {
                        base.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("修改失败！");
                    }
                }
                else if (this.bll.Add(this.model) > 0)
                {
                    base.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }
            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                new FrmSqlResult(new UserBLL().GetList(this.selectSql.Text, 0)).ShowDialog();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FrmUserReportDesign_Load(object sender, EventArgs e)
        {
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmUserReportDesign));
            this.label1 = new Label();
            this.ReportName = new TextBox();
            this.label2 = new Label();
            this.cbColumnsList = new CheckedListBox();
            this.ShowOrganName = new CheckBox();
            this.ShowUserName = new CheckBox();
            this.ShowTel = new CheckBox();
            this.ShowDate = new CheckBox();
            this.label3 = new Label();
            this.label4 = new Label();
            this.rbOffice = new RadioButton();
            this.rbBusiness = new RadioButton();
            this.label5 = new Label();
            this.selectSql = new TextBox();
            this.btnCancel = new Button();
            this.btnSave = new Button();
            this.btnTest = new Button();
            base.SuspendLayout();
            this.label1.AutoSize = true;
            this.label1.Location = new Point(0x45, 0x22);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x35, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "报表名称";
            this.ReportName.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.ReportName.BorderStyle = BorderStyle.FixedSingle;
            this.ReportName.Location = new Point(140, 30);
            this.ReportName.Name = "ReportName";
            this.ReportName.Size = new Size(0x1b4, 0x15);
            this.ReportName.TabIndex = 12;
            this.label2.AutoSize = true;
            this.label2.Location = new Point(0x45, 0x42);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x35, 12);
            this.label2.TabIndex = 13;
            this.label2.Text = "表头显示";
            this.cbColumnsList.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.cbColumnsList.FormattingEnabled = true;
            this.cbColumnsList.Location = new Point(140, 0x63);
            this.cbColumnsList.MultiColumn = true;
            this.cbColumnsList.Name = "cbColumnsList";
            this.cbColumnsList.ScrollAlwaysVisible = true;
            this.cbColumnsList.Size = new Size(0x1b4, 0xa4);
            this.cbColumnsList.TabIndex = 14;
            this.ShowOrganName.AutoSize = true;
            this.ShowOrganName.Checked = true;
            this.ShowOrganName.CheckState = CheckState.Checked;
            this.ShowOrganName.Location = new Point(140, 0x40);
            this.ShowOrganName.Name = "ShowOrganName";
            this.ShowOrganName.Size = new Size(0x48, 0x10);
            this.ShowOrganName.TabIndex = 15;
            this.ShowOrganName.Text = "报表单位";
            this.ShowOrganName.UseVisualStyleBackColor = true;
            this.ShowUserName.AutoSize = true;
            this.ShowUserName.Checked = true;
            this.ShowUserName.CheckState = CheckState.Checked;
            this.ShowUserName.Location = new Point(0x106, 0x40);
            this.ShowUserName.Name = "ShowUserName";
            this.ShowUserName.Size = new Size(60, 0x10);
            this.ShowUserName.TabIndex = 0x10;
            this.ShowUserName.Text = "填表人";
            this.ShowUserName.UseVisualStyleBackColor = true;
            this.ShowTel.AutoSize = true;
            this.ShowTel.Checked = true;
            this.ShowTel.CheckState = CheckState.Checked;
            this.ShowTel.Location = new Point(0x189, 0x40);
            this.ShowTel.Name = "ShowTel";
            this.ShowTel.Size = new Size(0x30, 0x10);
            this.ShowTel.TabIndex = 0x11;
            this.ShowTel.Text = "电话";
            this.ShowTel.UseVisualStyleBackColor = true;
            this.ShowDate.AutoSize = true;
            this.ShowDate.Checked = true;
            this.ShowDate.CheckState = CheckState.Checked;
            this.ShowDate.Location = new Point(0x1f8, 0x40);
            this.ShowDate.Name = "ShowDate";
            this.ShowDate.Size = new Size(0x48, 0x10);
            this.ShowDate.TabIndex = 0x12;
            this.ShowDate.Text = "填表日期";
            this.ShowDate.UseVisualStyleBackColor = true;
            this.label3.AutoSize = true;
            this.label3.Location = new Point(0x45, 0x63);
            this.label3.Name = "label3";
            this.label3.Size = new Size(0x35, 12);
            this.label3.TabIndex = 0x13;
            this.label3.Text = "显示字段";
            this.label4.AutoSize = true;
            this.label4.Location = new Point(0x45, 0x187);
            this.label4.Name = "label4";
            this.label4.Size = new Size(0x35, 12);
            this.label4.TabIndex = 20;
            this.label4.Text = "使用范围";
            this.rbOffice.AutoSize = true;
            this.rbOffice.Checked = true;
            this.rbOffice.Location = new Point(0x8d, 0x185);
            this.rbOffice.Name = "rbOffice";
            this.rbOffice.Size = new Size(0x47, 0x10);
            this.rbOffice.TabIndex = 0x15;
            this.rbOffice.TabStop = true;
            this.rbOffice.Text = "行政单位";
            this.rbOffice.UseVisualStyleBackColor = true;
            this.rbBusiness.AutoSize = true;
            this.rbBusiness.Location = new Point(0xfd, 0x185);
            this.rbBusiness.Name = "rbBusiness";
            this.rbBusiness.Size = new Size(0x47, 0x10);
            this.rbBusiness.TabIndex = 0x16;
            this.rbBusiness.Text = "事业单位";
            this.rbBusiness.UseVisualStyleBackColor = true;
            this.label5.AutoSize = true;
            this.label5.Location = new Point(0x4b, 0x11e);
            this.label5.Name = "label5";
            this.label5.Size = new Size(0x2f, 12);
            this.label5.TabIndex = 0x17;
            this.label5.Text = "筛选SQL";
            this.selectSql.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.selectSql.BorderStyle = BorderStyle.FixedSingle;
            this.selectSql.Location = new Point(140, 0x11c);
            this.selectSql.Multiline = true;
            this.selectSql.Name = "selectSql";
            this.selectSql.Size = new Size(0x1b4, 0x5d);
            this.selectSql.TabIndex = 0x18;
            this.btnCancel.BackColor = Color.White;
            this.btnCancel.FlatStyle = FlatStyle.Popup;
            this.btnCancel.Location = new Point(0x189, 0x1ab);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x4b, 0x17);
            this.btnCancel.TabIndex = 0x1a;
            this.btnCancel.Text = "关闭";
            this.btnCancel.UseVisualStyleBackColor = false;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnSave.BackColor = Color.White;
            this.btnSave.FlatStyle = FlatStyle.Popup;
            this.btnSave.Location = new Point(0xe1, 0x1ab);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new Size(0x4b, 0x17);
            this.btnSave.TabIndex = 0x19;
            this.btnSave.Text = "保存";
            this.btnSave.UseVisualStyleBackColor = false;
            this.btnSave.Click += new EventHandler(this.btnSave_Click);
            this.btnTest.BackColor = Color.White;
            this.btnTest.FlatStyle = FlatStyle.Popup;
            this.btnTest.Location = new Point(0x241, 0x162);
            this.btnTest.Name = "btnTest";
            this.btnTest.Size = new Size(0x2c, 0x17);
            this.btnTest.TabIndex = 0x1b;
            this.btnTest.Text = "测试";
            this.btnTest.UseVisualStyleBackColor = false;
            this.btnTest.Click += new EventHandler(this.btnTest_Click);
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            base.ClientSize = new Size(0x2a4, 0x1ce);
            base.Controls.Add(this.btnTest);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnSave);
            base.Controls.Add(this.selectSql);
            base.Controls.Add(this.label5);
            base.Controls.Add(this.rbBusiness);
            base.Controls.Add(this.rbOffice);
            base.Controls.Add(this.label4);
            base.Controls.Add(this.label3);
            base.Controls.Add(this.ShowDate);
            base.Controls.Add(this.ShowTel);
            base.Controls.Add(this.ShowUserName);
            base.Controls.Add(this.ShowOrganName);
            base.Controls.Add(this.cbColumnsList);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.ReportName);
            base.Controls.Add(this.label1);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FrmUserReportDesign";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "人员报表设计器";
            base.Load += new EventHandler(this.FrmUserReportDesign_Load);
            base.ResumeLayout(false);
            base.PerformLayout();
        }
    }
}

